﻿using System;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Commands;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Runtime.Serialization.DataContract.Enums;
using System.Collections.Generic;

namespace StockholmsStad.Windows.UI.Modules.Ceremony.Models
{
    public class CeremonyDetailsPresentationModel
    {
        public event EventHandler<DataEventArgs<CeremonyDetailsPresentationModel>> PrintedAllDocuments;
        public event EventHandler<DataEventArgs<CeremonyDetailsPresentationModel>> PrintedMarriageCertificate;
        public event EventHandler<DataEventArgs<CeremonyDetailsPresentationModel>> PrintedMarriageReport;
        public event EventHandler<DataEventArgs<CeremonyDetailsPresentationModel>> PrintedMarriageRegister;
        public event EventHandler<DataEventArgs<CeremonyDetailsPresentationModel>> Cancelled;
        public event EventHandler<DataEventArgs<CeremonyDetailsPresentationModel>> Confirmed;
        public DelegateCommand<object> PrintAllDocumentsCommand { get; private set; }
        public DelegateCommand<object> PrintMarriageCertificateCommand { get; private set; }
        public DelegateCommand<object> PrintMarriageReportCommand { get; private set; }
        public DelegateCommand<object> PrintMarriageRegisterCommand { get; private set; }
        public DelegateCommand<object> CancelCommand { get; private set; }
        public DelegateCommand<object> ConfirmedCommand { get; private set; }

        #region Properties
        
        public string StartEndTime
        {
            get {
                return string.Format("{0}/{1}", BookingDate.ToString("HH:mm"),
                                     BookingDate.AddMinutes(IntervalMinutes).ToString("HH:mm"));
            }
        }

        public bool ShowNotes
        {
            get
            {
                if(string.IsNullOrEmpty(Note))
                    return false;
                else
                    return true;
            }
        }

        public int TicketId { get; set; }
        public int CoupleId { get; set; }
        public int MinisterId { get; set; }
        public Minister Minister { get; set; }
        public bool ShouldCancel { get; set; }
        public DateTime BookingDate { get; set; }
        public Person Person1 { get; set; }
        public Person Person2 { get; set; }
        public MarriageCertificateType MarriageType { get; set; }
        public bool ImpedimentDoc { get; set; }
        public List<ValueIdPair> SpecialNeeds { get; set; }
        public ValueIdPair CeremonyLanguage { get; set; }
        public ValueIdPair CertificateLanguage { get; set; }
        public bool Interpreter { get; set; }
        public string Note { get; set; }
        public int IntervalMinutes { get; set; }
        public CeremonyType CeremonyType { get; set; }
        public TicketStatusType TicketStatus { get; set; }
        public bool IsCompleted { get; set; }

        #endregion

        #region Contructor
        public CeremonyDetailsPresentationModel()
        {
            CancelCommand = new DelegateCommand<object>(Cancel);
            PrintAllDocumentsCommand = new DelegateCommand<object>(PrintAllDocuments);
            PrintMarriageCertificateCommand = new DelegateCommand<object>(PrintMarriageCertificate);
            PrintMarriageRegisterCommand = new DelegateCommand<object>(PrintMarriageRegister);
            PrintMarriageReportCommand = new DelegateCommand<object>(PrintMarriageReport);
            ConfirmedCommand = new DelegateCommand<object>(Confirm);
        }
        #endregion

        private void Cancel(object obj)
        {
            OnCancel(new DataEventArgs<CeremonyDetailsPresentationModel>(this));
        }

        private void Confirm(object obj)
        {
            OnConfirm(new DataEventArgs<CeremonyDetailsPresentationModel>(this));
        }

        private void PrintAllDocuments(object obj)
        {
            OnPrintAllDocuments(new DataEventArgs<CeremonyDetailsPresentationModel>(this));
        }

        private void PrintMarriageCertificate(object obj)
        {
            OnPrintMarriageCertificate(new DataEventArgs<CeremonyDetailsPresentationModel>(this));
        }

        private void PrintMarriageRegister(object obj)
        {
            OnPrintMarriageRegister(new DataEventArgs<CeremonyDetailsPresentationModel>(this));
        }

        private void PrintMarriageReport(object obj)
        {
            OnPrintMarriageReport(new DataEventArgs<CeremonyDetailsPresentationModel>(this));
        }

        private void OnCancel(DataEventArgs<CeremonyDetailsPresentationModel> e)
        {
            var cancelHandler = Cancelled;

            if (cancelHandler != null)
                cancelHandler(this, e);
        }

        private void OnConfirm(DataEventArgs<CeremonyDetailsPresentationModel> e)
        {
            var confirmHandler = Confirmed;

            if (confirmHandler != null)
                confirmHandler(this, e);
        }

        private void OnPrintAllDocuments(DataEventArgs<CeremonyDetailsPresentationModel> e)
        {
            var printHandler = PrintedAllDocuments;

            if (printHandler != null)
                printHandler(this, e);
        }

        private void OnPrintMarriageCertificate(DataEventArgs<CeremonyDetailsPresentationModel> e)
        {
            var printHandler = PrintedMarriageCertificate;

            if (printHandler != null)
                printHandler(this, e);
        }

        private void OnPrintMarriageRegister(DataEventArgs<CeremonyDetailsPresentationModel> e)
        {
            var printHandler = PrintedMarriageRegister;

            if (printHandler != null)
                printHandler(this, e);
        }

        private void OnPrintMarriageReport(DataEventArgs<CeremonyDetailsPresentationModel> e)
        {
            var printHandler = PrintedMarriageReport;

            if (printHandler != null)
                printHandler(this, e);
        }
    }
}